package com.kai.controller;

import com.kai.Util.ResultMsg;
import com.kai.Util.ResultStatusCode;
import com.kai.entity.User;
import com.kai.mapper.first.MessageMapper;
import com.kai.mapper.first.UserMapper;
import com.kai.service.ipml.RedisService;
import com.kai.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by zhangkai on 18-7-31.
 *获取用户列表
 */
@Component
@RestController
@RequestMapping("/user")
public class UserController {

    private static Logger log = LoggerFactory.getLogger(UserController.class);


    @Resource
    private UserService userService;

    @Resource
    private MessageMapper messageMapper;

    @Resource
    private UserMapper userMapper;


    @Resource
    private RedisService redisService;
    /**
     * 获取分页列表
     * @param userName
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping (value = "/listpage")
    public Map<String, Object>  getListpage(String userName,int pageNum, int pageSize){

        Map<String,Object> map = new HashMap<String, Object>();

        if(userName !=null){
            map.put("userName",userName);
        }
        map.put("pageNum",pageNum);
        map.put("pageSize",pageSize);
        Map<String, Object>  list=userService.getListpage(map);
        redisService.set("user",list);
        return list;
    }

    /**
     * 获取用户信息
     */
    @GetMapping(value = "/getUserInfo")
    public ResultMsg getUserInfo(String token){

        Map<String, Object> map =new HashMap<String,Object>();
        map.put("userName",token);
        User users =userMapper.findUsersRoleByUsername(map);
        ResultMsg resultMsg = new ResultMsg(ResultStatusCode.OK.getCode(), ResultStatusCode.OK.getMsg(), users);

        return resultMsg;
    }

    /**
     * 获取列表list
     * @return
     */
    @PostMapping(value = "/list")
    public List<User>  getList(){

        List<User>  list=messageMapper.findMessageInfo();

        return list;
    }


    /**
     * 获取列表list
     * @return
     */
    @PostMapping (value = "/getUserList")
    public Map<String,Object>  getLists(){

        Map<String,Object> map = new HashMap<String, Object>();
        Map<String,Object> result=(Map<String,Object>)redisService.get("user");
        //map.put("count",result);
        return result;
    }

    /**
     * 获取列表list
     * @return
     */
    @PostMapping (value = "/insert")
    @ResponseBody
    public int  insert(@RequestBody User user){
        User u=new User();
        for (int i = 0; i <10 ; i++) {
            user.setUsername("springboot测试asdasdas（）阿三大赛大赛的"+i);
            user.setPassword("asd"+i);
            user.setRealName("阿三大赛大赛大赛大赛大赛大赛大大是");
        }
        return userMapper.saveUser(user);
    }
}